import Vuex from "vuex";
import Vue from "vue";
import cjy from "./cjy";
import zx from "./zx";
import wxh from "./wxh";
import lwk from "./lwk";
import gw from "./gw";
import xxy from "./xxy";
import qxz from "./qxz";
import zwy from "./zwy";
import dom from "../config/page-name";
import deepCopy from "../utils/deepCopy";
import jyy from './jyy';
import hzh from './hzh';
import lyj from './lyj';
import lxn from './lxn';
import {MGDialog} from "../ui-components";
import getPageName from "../utils/getPageName";
import {getPageNameOrVirtualPageName} from "../utils/getPageName";
// import singleManager from "../utils/getSingle";
Vue.use(Vuex);
let mDialog;
const store = new Vuex.Store({
    state: {
        DomTree: {

        },
        editConfig: {
            type: "",
            id: ""
        },
        com1Data: [""],
        com2Data: [""],
        //保存配置相关的参数
        configParams: {},
        //保存当前选中的模板信息
        currentTemplate: {},
        //保存某个item对象的副本
        pageDomItemObjCopy: "",
        //记录第一次进入页面默认刷新一次的标志
        firstInitDom: {},
        //style-components是否为抽屉样式
        styleComponentsState: "show",
        //style-components-cover的状态
        styleComponentsCoverState: false,
        //通过路由钩子设置页面name
        routerLifeCycleOfName: {},
        //请求商城控制数据状态
        storeConfigStatus: '',
        cartConfig: '',
        //用来监听android返回按钮的back标志位
        nativeBackFlagForPay: false,
        payS:''

    },
    getters: {
        com1Data(state) {
            return state.com1Data;
        },
        com2Data(state) {
            return state.com2Data;
        },
        getDomTree(state) {
            return state.DomTree;
        },
        editConfig: state => state.editConfig,
        //获取配置参数的列表
        getConfigParams: state => state.configParams,
        //获取当前选中模板的信息
        getCurrentTemplate: state => state.currentTemplate,
        //获取某个item对象的副本
        getPageDomItemObjCopy: state => {
            // 默认获取服务器中返回回来的
            // console.log("getPageName", getPageName());
            // return state.pageDomItemObjCopy ? state.pageDomItemObjCopy
            //     : Object.keys(state.currentTemplate).length ? JSON.stringify(JSON.parse(state.currentTemplate.tpConfig)[getPageName()])
            //         : "";
            return state.pageDomItemObjCopy ? state.pageDomItemObjCopy
                : Object.keys(state.currentTemplate).length ? state.currentTemplate.tpConfig
                    : "";
        },
        //获取记录第一次进入页面默认刷新一次的标志
        getFirstInitDom: state => state.firstInitDom,
        //获取style-components是否为抽屉样式
        getStyleComponentsState: state => state.styleComponentsState,
        //获取style-components-cover的状态
        getStyleComponentsCoverState: state => state.styleComponentsCoverState,
        //路由钩子设置页面name
        getRouterLifeCycleOfName: state => state.routerLifeCycleOfName,
        //商城控制数据的申请状态
        cartConfig : state => state.cartConfig,
        storeConfigStatus:state => state.storeConfigStatus,
        // 是否等待支付
        payS:state => state.payS,

        //获取用来监听android返回按钮的back标志位
        getNativeBackFlagForPay: state => state.nativeBackFlagForPay,

    },

    mutations: {
        setCom1Data(state) {
            console.log("新增1");
            // state.com1Data.length = state.com1Data.length + 1;
            state.com1Data.push("");
        },
        setCom2Data(state) {
            console.log("新增2");
            // state.com1Data.length = state.com1Data.length + 1;
            state.com2Data.push("");
        },
        setDomTree(state, obj) {
            state.DomTree = Object.assign({}, state.DomTree, obj);
            // console.log("--0-0--0-0-0-0-0-0-", state.DomTree);
        },
        setEditConfigVuex(state, obj) {
            // console.log("setEditConfig", params.context.setEditConfig);
            state.editConfig = obj;

        },
        setConfigParams(state, obj) {
            state.configParams = obj;
        },
        //改变当前选中的模板配置
        setCurrentTemplate(state, obj) {
            // console.log("setCurrentTemplate", obj);
            // state.currentTemplate = Object.assign({}, state.currentTemplate, obj);
            state.currentTemplate = obj;
        },
        //改变保存的副本
        setPageDomItemObjCopy(state, obj) {
            state.pageDomItemObjCopy = obj;
        },
        //改变记录第一次进入页面默认刷新一次的标志
        setFirstInitDom(state, obj) {
            state.firstInitDom = Object.assign({}, state.firstInitDom, obj);
        },
        //设置style-components是否为抽屉样式
        setStyleComponentsState(state, val) {
            state.styleComponentsState = val;
        },
        //设置style-components-cover的状态
        setStyleComponentsCoverState(state, val) {
            state.styleComponentsCoverState = val;
        },
        //路由钩子设置页面name
        setRouterLifeCycleOfName(state, obj) {
            state.routerLifeCycleOfName = obj;
        },
        SET_STORE_CONFIG_LOAD_STATUS(state,data) {
            state.storeConfigStatus = data

        },
        SET_STORE_CONFIG (state,status) {
            state.cartConfig =  status
        },
        //设置用来监听android返回按钮的back标志位
        setNativeBackFlagForPay(state, flag) {
            state.nativeBackFlagForPay = flag;
        }

    },
    actions: {
        /**
         *
         * @param {*} store
         * @param {*} params:{{id, type}, context}
         */
        changeEditItem({commit, getters}, params) {
            //处理两种类型，一种为currentPage，一种为backHome
            //用户更改了配置项，新增或删除组件，调换组件的顺序都要提示用户做了更改
            //注意，给副本赋值的时候，一定要用对象深克隆
            //判断如果copy为空或者与当前原本对比无变化，则给copy赋值，以及设置编辑页面显示什么
            let context = params.context;
            //obj为一个自定义的对象，能拿到组件的type和组件的id
            let obj = params.obj;
            let handleType = params.handleType ? params.handleType : "currentPage";
            // console.log(context.getCurrentVDO);
            // console.log(params.obj);
            //获取副本，可能为空
            // console.log(getters.getPageDomItemObjCopy);
            let copy = getters.getPageDomItemObjCopy ? JSON.stringify(JSON.parse(getters.getPageDomItemObjCopy)[getPageNameOrVirtualPageName()]) : "";
            // console.log("copy", copy);
            //获取源
            // let origin = context.getCurrentVDO(copy.id);
            let origin = JSON.stringify(getters.getDomTree[getPageNameOrVirtualPageName()]);
            // console.log("copy-0-0-0-0-0-0-0-0-0", copy);
            //比对副本和源，如果两者相同或者副本为空，则为真，将源进行深克隆赋值给副本
            //即用户编辑后会更改源而不会更改副本，因为源就是从当前domTree获取的
            //所以当用户放弃修改后，将源还原到副本即可
            // if (!Object.keys(getters.getPageDomItemObjCopy).length || context.judgeObjEqual(copy, origin)) {
            if (!copy || copy === origin) {
                if (handleType === "currentPage") {
                    // let newOrigin = deepCopy(context.getCurrentVDO(obj.id));
                    //设置编辑页面显示什么
                    commit("setEditConfigVuex", obj);
                    commit("setPageDomItemObjCopy", JSON.stringify(getters.getDomTree));
                } else if (handleType === "backHome") {
                    commit("setCurrentTemplate", {});
                    window.location.hash = "/?env=e";
                    // setCurrentTemplate
                }

            } else {
                //提示用户保存更改
                if (!mDialog) {
                    //配置dialog的参数
                    mDialog = new MGDialog({
                        msg: "要保存当前更改吗？",
                        //配置确认按钮点击事件
                        confirmFn() {
                            //隐藏对话框
                            mDialog.hideDialog();
                            let success = (data) => {
                                alert("模板保存成功");
                                //重新给Vuex里的数据赋值
                                context.resetTemplateParams(data);
                                //更新副本
                                // let newCopy = deepCopy(origin);
                                // commit("setPageDomItemObjCopy", newCopy);
                                commit("setPageDomItemObjCopy", JSON.stringify(getters.getDomTree));

                            }
                            let fail = () => {
                                alert("模板保存失败");
                            }
                            context.updateDomTreeToDB(success, fail);
                        },
                        //配置取消按钮事件
                        cancelFn() {
                            //点击了取消按钮
                            console.log("点击了cancel");
                            mDialog.hideDialog();
                        },
                        //配置放弃按钮事件
                        abortFn() {
                            //点击了放弃修改按钮
                            console.log("点击了放弃修改");
                            //放弃修改就把源会退到副本
                            // console.log("copy", copy);
                            // context.setEditConfig(copy.id, copy.config);
                            let domTree = getters.getDomTree;
                            domTree[getPageNameOrVirtualPageName()] = JSON.parse(copy);
                            commit("setDomTree", domTree);
                            //同时清空副本
                            commit("setPageDomItemObjCopy", "");
                            // commit("setEditConfigVuex", obj);
                            mDialog.hideDialog();
                        }
                    });
                    mDialog.showDialog();
                } else {
                    mDialog.showDialog();
                }
            }
        },
        setStoreConfig ({commit},data) {
            commit( 'SET_STORE_CONFIG',data)
            commit( 'SET_STORE_CONFIG_LOAD_STATUS','loaded')
        }

    },
    modules: {
        gw,
        xxy,
        cjy,
        lwk,
        wxh,
        zx,
        qxz,
        jyy,
        zwy,
        hzh,
        lyj,
        lxn
    }
});

export default store;
